/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.manager.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Set;
import linkeddata.arida.ufc.br.search.HierarchyOfClasses;
import linkeddata.arida.ufc.br.util.OWLUtils;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.util.DefaultPrefixManager;

/**
 *
 * @author jeovane
 */
public class BuildsSparqlQueryAux extends OWLUtils {

    private HierarchyOfClasses hoc;

    public BuildsSparqlQueryAux(OWLOntology ontology, OWLReasoner reasoner, DefaultPrefixManager pm) {
        hoc = new HierarchyOfClasses(ontology, reasoner, pm);
    }

    public ArrayList<String> isRange(String domain, String range) {
        setSetDomain(domain);
        setArrayRange(range);

        for (String srtDomain : getSetDomain()) {
            for (String srtRange : getArrayRange()) {
                if (srtRange.equalsIgnoreCase(srtDomain)) {
                    setArrayIsRange(srtDomain);
                }
            }
        }
        return getArrayIsRange();
    }

    // Verifica se a classe é Range
    public String isRange() {
        String domain = "";
        for (String strDomain : getArrayIsRange()) {
            domain = strDomain;
        }
        return domain;
    }

    // Encontra o Domain pra um determinado Range
    public String findDomain() {
        String domain = "";
        for (String srtDomain : getSetDomain()) {
            if (!isRange().equalsIgnoreCase(srtDomain)) {
                domain = srtDomain;
            }
        }
        return domain;
    }

    // Obtém as classes que foram selecionadas para serem OPTIONAL @buildsOptionalKeys e @buildsOptionalValues
    public Set<String> buildsOptionalKeys(String jsonTextQuery, String jsonTextOptional) {
        Set<String> optionalKey = new HashSet<String>();
        if (jsonTextOptional != null && jsonTextQuery != null) {
            if (BuildsMapJson.JSONMap(jsonTextQuery).keySet()
                    .containsAll(BuildsMapJson.JSONMap(jsonTextOptional).keySet())) {
                optionalKey.addAll(BuildsMapJson.JSONMap(jsonTextOptional).keySet());
            }
        }
        return optionalKey;
    }

    public LinkedList<String> buildsOptionalValues(String jsonTextQuery, String jsonTextOptional) {
        LinkedList<String> optionalValues = new LinkedList<String>();
        if (jsonTextOptional != null) {
            for (String optValues : buildsOptionalKeys(jsonTextQuery, jsonTextOptional)) {
                optionalValues.addAll((LinkedList<String>) BuildsMapJson.JSONMap(jsonTextOptional).get(optValues));
            }
        }
        return optionalValues;
    }

    // Ordena as classes de acordo com a hierarquia da Ontologia
    public Set<String> classHierarchyKey(String jsonTextQuery) {
        Set<String> setKeys = new LinkedHashSet<String>();
        Set<String> jsonKeys = BuildsMapJson.JSONMap(jsonTextQuery).keySet();

        for (String node : hoc.CorrectHierarchyOfClasses()) {
            if (jsonKeys.contains(node)) {
                setKeys.add(node);
            }
        }
        return setKeys;
    }

    // Obtém os valores que estão no Map
    public LinkedList<String> classHierarchyValue(String jsonTextQuery, String key) {
        LinkedList<String> linkedValues = (LinkedList<String>) BuildsMapJson.JSONMap(jsonTextQuery).get(key);
        return linkedValues;
    }
}
